package cn.wolfcode.wolf2w.strategy.service.impl;

import cn.wolfcode.wolf2w.destination.entity.TaDestination;
import cn.wolfcode.wolf2w.strategy.entity.Strategy;
import cn.wolfcode.wolf2w.strategy.entity.StrategyRank;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyMapper;
import cn.wolfcode.wolf2w.strategy.mapper.StrategyRankMapper;
import cn.wolfcode.wolf2w.strategy.query.StrategyRankQuery;
import cn.wolfcode.wolf2w.strategy.service.IStrategyRankService;
import cn.wolfcode.wolf2w.strategy.vo.ThemeVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 攻略排行Service业务层处理
 * 
 * @author libai
 * @date 2024-07-24
 */
@Service
@Transactional
public class StrategyRankServiceImpl extends ServiceImpl<StrategyRankMapper, StrategyRank> implements IStrategyRankService {
    @Autowired
    private StrategyRankMapper strategyRankMapper;

    @Autowired
    private StrategyMapper strategyMapper;

    @Override
    public IPage<StrategyRank> queryPage(StrategyRankQuery qo) {
        IPage<StrategyRank> page = new Page<>(qo.getCurrentPage(), qo.getPageSize());
        return lambdaQuery()
                .page(page);
    }

    @Override
    public List<StrategyRank> queryByType(Integer type) {
        //TODO 未完成业务
        List<StrategyRank> list = lambdaQuery()
                .inSql(StrategyRank::getStatisTime, "select max(statis_time) from  ta_strategy_rank")
                .eq(StrategyRank::getType, type)
                .orderByDesc(StrategyRank::getStatisnum)
                .list();
        return list;
    }

    @Override
    public List<ThemeVO> queryTheme() {
        List<ThemeVO> vos = new ArrayList<>();
        QueryWrapper<Strategy> qw = new QueryWrapper<>();
        qw.select("theme_name, GROUP_CONCAT(DISTINCT dest_id) ids, GROUP_CONCAT(DISTINCT dest_name) names");
        qw.groupBy("theme_name");//分组
        List<Map<String, Object>> maps = strategyMapper.selectMaps(qw);
        for (Map<String, Object> map : maps) {
            String themeName = (String) map.get("theme_name");
            String ids = (String) map.get("ids");
            String names = (String) map.get("names");
            String[] idArr = ids.split(",");
            String[] nameArr = names.split(",");
            List<TaDestination> dests = new ArrayList<>();
            for (int i = 0; i < idArr.length; i++) {
                Long id = Long.valueOf(idArr[i]);
                String name = nameArr[i];
                TaDestination dest = new TaDestination();
                dest.setId(id);
                dest.setName(name);
                dests.add(dest);
            }
            ThemeVO themeVO = new ThemeVO();
            themeVO.setThemeName(themeName);
            themeVO.setDests(dests);
            vos.add(themeVO);
        }
        return vos;
    }

    @Override
    public void updateStrategyRank(int type,Date date){
        LambdaQueryWrapper<Strategy> qw = new LambdaQueryWrapper<>();
        if (type == 1){
            qw.eq(Strategy::getIsabroad,1);
            qw.orderByDesc(Strategy::getViewnum);
        } else if (type ==2) {
            qw.eq(Strategy::getIsabroad,0);
            qw.orderByDesc(Strategy::getViewnum);
        }else if (type == 3){

            qw.orderByDesc(Strategy::getViewnum);
        }
        qw.last("limit 10");
        List<Strategy> strategyList = strategyMapper.selectList(qw);
        List<StrategyRank> ranks = new ArrayList<>();
        for (Strategy strategy : strategyList) {
            StrategyRank rank = new StrategyRank();
            rank.setDestId(strategy.getDestId());
            rank.setDestName(strategy.getDestName());
            rank.setStrategyId(strategy.getId());
            rank.setStrategyTitle(strategy.getTitle());
            rank.setType((long)type);
            rank.setStatisnum(strategy.getViewnum());
            rank.setStatisTime(date);
            ranks.add(rank);
        }
        saveOrUpdateBatch(ranks);
    }
}
